package com.study.guide.codec.MessagePack;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class TimeClientHandler extends SimpleChannelInboundHandler<Object> {

	private final int  sendNumber;
	public TimeClientHandler(int sendNumber) {
		this.sendNumber = sendNumber;
	}
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
		System.out.println("Client receive the magpack message :" + msg);
		//ctx.writeAndFlush(msg);
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		UserInfo[] infos =userInfo();
		for(UserInfo infoE :infos) {
			ctx.writeAndFlush(infoE);
		}
		//ctx.flush();
		//System.out.println("------------客户端激活");
	}

	private UserInfo[] userInfo() {
		UserInfo[] userInfos =new UserInfo[sendNumber];
		UserInfo userInfo =null;
		for(int i=0;i<sendNumber;i++) {
			userInfo = new UserInfo();
			userInfo.setAge(i);
			userInfo.setName("ABCDEFG---->"+i);
			userInfos[i] = userInfo;
		}
		return userInfos;
	}


	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		ctx.close();
	}
}
